/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.gif;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.util.LinkedHashMap;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToolBar;

import twaver.AlarmSeverity;
import twaver.Element;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.InteractionEvent;
import twaver.network.InteractionListener;
import twaver.network.TNetwork;
import twaver.network.ui.Attachment;
import demo.DemoPane;

public class GIFImageDemo extends DemoPane implements ActionListener{
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);

    public GIFImageDemo() {
    	network.setEnableDeleteWithKeyboard(true);
        TUIManager.registerAttachment("fireworks", "/demo/network/topo/gif/images/fireworks.gif");
        String[] twavers = new String[]{"T", "W", "A", "V", "E", "R"}; 
        TUIManager.registerAttachment("T", "/demo/network/topo/gif/images/T.gif");
        TUIManager.registerAttachment("W", "/demo/network/topo/gif/images/W.gif");
        TUIManager.registerAttachment("A", "/demo/network/topo/gif/images/A.gif");
        TUIManager.registerAttachment("V", "/demo/network/topo/gif/images/V.gif", TWaverConst.POSITION_RIGHT, 10, -30);
        TUIManager.registerAttachment("E", "/demo/network/topo/gif/images/E.gif", TWaverConst.POSITION_RIGHT, 10, 30);
        TUIManager.registerAttachment("R", "/demo/network/topo/gif/images/R.gif", TWaverConst.POSITION_RIGHT, 60, 30);
        
		JPanel panel1 = new JPanel(new FlowLayout());
		ButtonGroup group = new ButtonGroup();
		boolean value = network.isElementTransparentAreaSelectable();
		
		JRadioButton button = new JRadioButton("consider transparent Area", !value);
		button.setForeground(Color.RED);
		button.addActionListener(this);
		group.add(button);
		panel1.add(button);
	
		button = new JRadioButton("ignore transparent Area", value);
		button.setForeground(Color.BLUE);
		button.addActionListener(this);
		group.add(button);
		panel1.add(button);
    	
		JPanel panel2 = new JPanel(new FlowLayout());
		group = new ButtonGroup();
		value = network.isEnableMouseWheelToZoom();
		
		button = new JRadioButton("enable mouse wheel zoom", value);
		button.setForeground(Color.RED);
		button.addActionListener(this);
		group.add(button);
		panel2.add(button);
		
		button = new JRadioButton("disable mouse wheel zoom", !value);
		button.setForeground(Color.BLUE);
		button.addActionListener(this);
		button.setSelected(true);
		group.add(button);
		panel2.add(button);
		
		this.add(panel1, BorderLayout.NORTH);
		this.add(panel2, BorderLayout.SOUTH);
        this.add(network, BorderLayout.CENTER);

        network.setColorBackground(Color.white);
        
        JToolBar toolbar = network.getToolbar();
        toolbar.setOrientation(JToolBar.VERTICAL);
        network.remove(toolbar);
        network.add(toolbar, BorderLayout.WEST);

        Rectangle bounds = new Rectangle(0, 0, 160, 160);
        float w = bounds.width * 2;
		float h = bounds.height * 2;
        float ox = bounds.x + w/4;
        float oy = bounds.y + h/4;
        final GeneralPath path = new GeneralPath();
        path.moveTo(ox - w / 4.0f, oy - h / 12.0f);
        path.lineTo(ox + w / 4.0f, oy - h / 12.0f);
        path.lineTo(ox - w / 6.0f, oy + h / 4.0f);
        path.lineTo(ox +     0.0f, oy - h / 4.0f);
        path.lineTo(ox + w / 6.0f, oy + h / 4.0f);
        path.closePath();
        
        network.putClientProperty(TWaverConst.NETWORK_MAGNIFIER_SHAPE_MAP, new LinkedHashMap(){
        	{
                put("Ellipse", new Ellipse2D.Double(0, 0, 160, 120));
                put("Round", new Ellipse2D.Double(0, 0, 160, 160));
                put("Rectangle", new Rectangle(0, 0, 160, 120));
                put("Square", new Rectangle(0, 0, 160, 160));
                put("Star", path);
        	}
        });
        network.putClientProperty(TWaverConst.NETWORK_MAGNIFIER_SHAPE, path);
        network.putClientProperty(TWaverConst.NETWORK_FULL_SCREEN_KEYBOARD, "F3");
        
        Node printer = new Node();
        printer.setLocation(100, 80);
        printer.getAlarmState().addNewAlarm(AlarmSeverity.CRITICAL);
        printer.putAlarmBalloonXoffset(-5);
        printer.putAlarmBalloonYoffset(5);
        printer.setImage("/demo/network/topo/gif/images/printer.gif");
        printer.setName("Printer");
        printer.putLabelColor(Color.green.darker());
        printer.putLabelFont(new Font("Forte", Font.BOLD, 20));
        network.getDataBox().addElement(printer);

        final Node computer = new Node();
        computer.setLocation(300, 100);
        computer.setImage("/demo/network/topo/gif/images/computer.gif");
        computer.setName("Computer");
        computer.putLabelColor(Color.blue.darker());
        computer.putLabelFont(new Font("Forte", Font.BOLD, 20));
        network.getDataBox().addElement(computer);

        final Node man = new Node();
        man.setLocation(100, 200);
        man.setImage("/demo/network/topo/gif/images/man.gif");
        man.setName("Ooh, come on ...");
        man.putLabelColor(Color.black);
        man.putLabelFont(new Font("Forte", Font.PLAIN, 15));
        network.getDataBox().addElement(man);

        Link link1 = new Link(computer, printer);
        link1.setLinkType(TWaverConst.LINK_TYPE_FLEXIONAL);
        link1.putLinkWidth(6);
        link1.putLinkColor(Color.white);
        link1.putLinkOutlineColor(Color.black);
        link1.putLinkFlowing(true);
        link1.putLinkFlowingWidth(5);
        link1.putLinkFlowingColor(Color.black);
        network.getDataBox().addElement(link1);

        Link link2 = new Link(computer, man);
        link2.setLinkType(TWaverConst.LINK_TYPE_ORTHOGONAL);
        link2.putLinkWidth(8);
        link2.putLinkColor(Color.orange);
        link2.putLinkOutlineColor(Color.red.brighter());
        link2.putLinkFlowing(true);
        link2.putLinkFlowingConverse(true);
        link2.putLinkFlowingWidth(8);
        link2.putLinkFlowingColor(Color.red);
        network.getDataBox().addElement(link2);
        
        computer.addAttachment("fireworks");
        for(int i=0; i<twavers.length; i++){
        	man.addAttachment(twavers[i]);
        }
        
        network.addInteractionListener(new InteractionListener(){
			public void interactionPerformed(InteractionEvent event) {
				if(event.getMouseEvent() == null){
					return;
				}
				if(event.getMouseEvent().getClickCount() == 2){
					if(InteractionEvent.ELEMENT_CLICKED == event.getType()){			
						Element element = (Element)event.getSource();
						String name = element.getName();
						if(name == null){
							name = TWaverUtil.getClassNameWithoutPackage(element.getClass());
						}
						JOptionPane.showMessageDialog(network, name + " is double clicked.");
					}
					else if(InteractionEvent.ATTACHMENT_CLICKED == event.getType()){
						Attachment attachment = (Attachment)event.getSource();
						String name = attachment.getName();
						if(name == null){
							name = TWaverUtil.getClassNameWithoutPackage(attachment.getClass());
						}else{
							name = "'" + name + "' attachment";
						}
						JOptionPane.showMessageDialog(network, name + " is double clicked.");
					}
				}
			}
        });

    }

	public void actionPerformed(ActionEvent e) {
		if(((JRadioButton)e.getSource()).getText().startsWith("enable")){
			network.setEnableMouseWheelToZoom(true);
		}else{
			network.setEnableMouseWheelToZoom(false);
		}
		if(((JRadioButton)e.getSource()).getText().startsWith("ignore")){
			network.setElementTransparentAreaSelectable(true);
		}else{
			network.setElementTransparentAreaSelectable(false);
		}
	}

}